package com.jushu.storbox.entities;

import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.database.IBaseDao;
import com.jushu.storbox.database.IFileDao;
import com.jushu.storbox.database.IShareINodeDao;
import com.huawei.sharedrive.sdk.android.modelV2.request.Thumbnail;
import com.huawei.sharedrive.sdk.android.modelV2.response.INodeShareV2;

public class ShareINode extends FileFolderInfo {
	
	private static final long serialVersionUID = 5733346286413536567L;
	
	private int itemViewFlag = 0;
	
	public int getItemViewed() {
		return itemViewFlag;
	}

	public void setItemViewed(int itemViewFlag) {
		this.itemViewFlag = itemViewFlag;
	}

	@Override
	public void valueOf(INodeShareV2 fileInfo) {
		setItemViewed(IShareINodeDao.ITEM_NOT_VIEWED);
		setType(fileInfo.getType());
		setOwnerBy(fileInfo.getOwnerId().toString());
		setOwnerName(fileInfo.getOwnerName());
		setSharedUserId(fileInfo.getSharedUserId());
		setSharedUserType(fileInfo.getSharedUserType());
		setSharedUserName(fileInfo.getSharedUserName());
		setSharedUserLoginName(fileInfo.getSharedUserLoginName());
		setSharedDepartment(fileInfo.getSharedUserDescrip());
		setiNodeId(fileInfo.getNodeId().toString());
		setId(getiNodeId());
		setName(fileInfo.getName());
		setModifiedAt(fileInfo.getModifiedAt());
		setModifiedBy(fileInfo.getModifiedBy());
		setStatus(fileInfo.getStatus().toString());
		setSize(fileInfo.getSize());
		List<Thumbnail> thumbnails = fileInfo.getThumbnailUrlList();
		if(thumbnails != null && !thumbnails.isEmpty()) {
			setThumbnailURL(thumbnails.get(0).getThumbnailUrl());
		}
		setRoleName(fileInfo.getRoleName());
		setTransStatus(Constant.DOWNLOAD_NON_UPDATE_DB);
		setSync(false);
		setIsInode(1);
	}

	
	public ContentValues toContentValues() {
		ContentValues cv = new ContentValues();
		cv.put(IShareINodeDao.itemViewed, getItemViewed());
		cv.put(IBaseDao.id, getId());
		cv.put(IBaseDao.owner_by, getOwnerId());
		cv.put(IBaseDao.inodeid, Integer.valueOf(getId()));
		cv.put(IBaseDao.file_name, getName());
		cv.put(IBaseDao.modifiedAt, getModifiedAt());
		cv.put(IBaseDao.modified_by, getModifiedBy());
		cv.put(IBaseDao.ownerName, getOwnerName());
		cv.put(IBaseDao.ownerId, getOwnerId());
		cv.put(IBaseDao.file_size, getSize());
		cv.put(IBaseDao.status, getStatus());
		cv.put(IBaseDao.type, getType());
		cv.put(IBaseDao.sharedUserId, getSharedUserId());
		cv.put(IBaseDao.sharedUserLoginName, getSharedUserLoginName());
		cv.put(IBaseDao.sharedUserName, getSharedUserName());
		cv.put(IBaseDao.sharedUserType, getSharedUserType());
		cv.put(IBaseDao.trans_status, getTransStatus());
		cv.put(IBaseDao.isSync, isSync() ? 1 : 0);
		return cv;
	}
	
	public void fromCursor(Cursor cursor) {
		setItemViewed(cursor.getInt(cursor.getColumnIndex(IShareINodeDao.itemViewed)));
		setType((byte) cursor.getInt(cursor.getColumnIndex(IBaseDao.type)));
		setOwnerBy(cursor.getString(cursor.getColumnIndex(IBaseDao.owner_by)));
		setOwnerName(cursor.getString(cursor.getColumnIndex(IBaseDao.ownerName)));
		setSharedUserId(cursor.getLong(cursor.getColumnIndex(IBaseDao.sharedUserId)));
		setSharedUserType(cursor.getString(cursor.getColumnIndex(IBaseDao.sharedUserType)));
		setSharedUserName(cursor.getString(cursor.getColumnIndex(IBaseDao.sharedUserName)));
		setSharedUserLoginName(cursor.getString(cursor.getColumnIndex(IBaseDao.sharedUserLoginName)));
		setSharedDepartment(cursor.getString(cursor.getColumnIndex(IBaseDao.shareDepartment)));
		setiNodeId(""+cursor.getInt(cursor.getColumnIndex(IBaseDao.inodeid)));
		setId(getiNodeId());
		setName(cursor.getString(cursor.getColumnIndex(IBaseDao.file_name)));
		setModifiedAt(cursor.getLong(cursor.getColumnIndex(IBaseDao.modifiedAt)));
		setModifiedBy(cursor.getString(cursor.getColumnIndex(IBaseDao.modified_by)));
		setRoleName(cursor.getString(cursor.getColumnIndex(IBaseDao.roleName)));
		setStatus(cursor.getString(cursor.getColumnIndex(IBaseDao.status)));
		setSize(cursor.getLong(cursor.getColumnIndex(IBaseDao.file_size)));
		setThumbnailURL(cursor.getString(cursor.getColumnIndex(IBaseDao.fileIcon)));
		setTransStatus(cursor.getInt(cursor.getColumnIndex(IBaseDao.trans_status)));
		setSync(cursor.getInt(cursor.getColumnIndex(IBaseDao.isSync)) == 1);
		
		setIsInode(1);
	}
	
	public ContentValues getIdentityCV() {
		ContentValues cv = super.toContentValues();
		cv.put(IFileDao.file_true_md5, getLocalIdentity());
		return cv;
	}
	
}
